package com.guigu.www.cxk.controller;

import com.guigu.www.cxk.service.CXKLoginService;
import com.guigu.www.pojo.Member;
import com.guigu.www.pojo.MemberCaseHistory;
import com.guigu.www.pojo.MemberTrends;
import com.guigu.www.utils.FtpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

//会员表
@Controller
@RequestMapping("/logincon")
public class CXKLoginController {
    @Autowired
    CXKLoginService loginService;

    /*登录手机号密码验证*/
    @PostMapping
    @ResponseBody
    public Member LoginMember(@RequestBody Member member){
        return loginService.LoginMember(member);
    }

    /*注册会员*/
    @PostMapping("/LogonMamber")
    @ResponseBody
    public void LogonMamber(@RequestBody Member member){
        /*注册会员*/
        loginService.LogonMember(member);
        /*根据注册会员的手机号查询刚注册的信息*/
        Member member1 = loginService.SelectMePhone(member.getMemPhone());
        /*给会员动态信息添加会员id*/
        MemberTrends memberTrends = new MemberTrends();
        memberTrends.setMemId(member1.getMemId());
        loginService.saveMember(memberTrends);
        /*给会员病例表添加会员id*/
        MemberCaseHistory memberCaseHistory = new MemberCaseHistory();
        memberCaseHistory.setMemId(member1.getMemId());
        loginService.saveCaseHistory(memberCaseHistory);
    }

    /*根据用户查询*/
    @PostMapping("/MeMberUser/{userid}")
    @ResponseBody
    public Member memberUser(@PathVariable int userid){
        return loginService.MemberUser(userid);
    }

    /*编辑会员个人信息*/
    @PostMapping("/UpdateMemberUser")
    @ResponseBody
    public int UpdateMemberUser(@RequestBody Member member){
        return loginService.UpdateMenmber(member);
    }

    /*编辑个性标签*/
    @PostMapping("/Publish")
    @ResponseBody
    public int UpdatePublish(@RequestBody Member member){
        return loginService.UpdatePublish(member);
    }

    /*更换头像*/
    @PostMapping("/MenmberImg/{userid}")
    @ResponseBody
    public boolean MenmberImg (@PathVariable int userid, MultipartFile imgtou) throws IOException {
        if (imgtou.isEmpty()) {
            return false;
        } else {
            // 获取上传的文件流
            InputStream inputStream = imgtou.getInputStream();

            //String parentPath = "F:\\s3GraduateProject\\physical_project\\src\\assets\\touxiang\\";//获取文件原名
            String originalFilename = imgtou.getOriginalFilename();
            //System.out.println(originalFilename);
            //获取源文件前缀
            String fileNamePrefix = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            //获取源文件后缀
            String fileNameSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            // 将源文件前缀之后加上时间戳避免重名
            String newFileNamePrefix = fileNamePrefix + new Date().getTime();
            //得到上传后新文件的文件名
            String newFileName = newFileNamePrefix + fileNameSuffix;
            boolean b = FtpUtil.uploadFile(newFileName, inputStream);
            return loginService.UpdateImg(newFileName, userid);
        }
    }

    /*给账号做唯一操作*/
    @PostMapping("/WeiyiAccount/{phone}")
    @ResponseBody
    public Member WeiyiAccount(@PathVariable String phone){
        return loginService.selectAccount(phone);
    }

    /*根据手机号验证*/
    @PostMapping("updatephone/{mephone}")
    @ResponseBody
    public Member updatepassword(@PathVariable String mephone){
        return loginService.updateMePhone(mephone);
    }

    /*根据用户人编号修改密码*/
    @PostMapping("updatemepassword/{mid}/{password}")
    @ResponseBody
    public int updatemepassword(@PathVariable int mid,@PathVariable String password){
        System.out.println(mid+" "+password);
        return loginService.updateMePassword(mid, password);
    }

    /*根据会员id查询会员动态信息*/
    @PostMapping("/selecttran/{sid}")
    @ResponseBody
    public MemberTrends selecttran(@PathVariable int sid){
        return loginService.SelectTrends(sid);
    }

    /*修改会员动态信息表*/
    @PostMapping("/updatecasetran/{sid}")
    @ResponseBody
    public int updatecasetran(@PathVariable int sid,@RequestBody MemberTrends memberTrends){
       return loginService.updatecase(memberTrends,sid);
    }

    /*根据会员id查询会员病例*/
    @PostMapping("selectcasehistory/{sid}")
    @ResponseBody
    public MemberCaseHistory selectcasehistory(@PathVariable int sid){
       return loginService.selectcasehistoty(sid);
    }

    /*修改会员病例*/
    @PostMapping("updatecasehistroy/{sid}")
    @ResponseBody
    public int updatecasehistroy(@RequestBody MemberCaseHistory memberCaseHistory,@PathVariable int sid){
        return loginService.updatecasehistroy(memberCaseHistory,sid);
    }
}
